package com.ntschy.common.utils;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

/**
 *  @Description: 中研通用加密工具 RSA+ASE+SHA256(非对称加密（对称秘钥），对称加密数据，Sha256消息摘要，RSA签名)
 *  @author  wh.huang  DateTime 2018年11月15日 下午3:00:21
 *  @version 1.0
 */
public class EncryptUtil {

    // 加密数据和秘钥的编码方式
    public static final String UTF_8 = "UTF-8";

    // 填充方式
    public static final String AES_ALGORITHM = "AES/CFB/PKCS5Padding";
    public static final String RSA_ALGORITHM = "RSA/ECB/PKCS1Padding";
    public static final String RSA_ALGORITHM_NO_PADDING = "RSA";


    public static final String KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDu+0d3N2VxZ39eM2tqXjiIxNkWPQcOkvtAhPwRXExiEw1oG3Qllejxf/4b72CTX7mNfgQV7hVBaIkCv836tcbAeS14CKEibYg41x5b9+koo/r755My8q/YCp72yLkq/KpMAQw6t66eLI0BayWiOvXwjG5Ii8lSjJZkxm/qTCNgiQIDAQAB";

    // 初始化私钥 保存成常量或数据库纪录
    public static final String PRIVATE_KEY_STR = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAO77R3c3ZXFnf14z\n" +
            "a2peOIjE2RY9Bw6S+0CE/BFcTGITDWgbdCWV6PF//hvvYJNfuY1+BBXuFUFoiQK/\n" +
            "zfq1xsB5LXgIoSJtiDjXHlv36Sij+vvnkzLyr9gKnvbIuSr8qkwBDDq3rp4sjQFr\n" +
            "JaI69fCMbkiLyVKMlmTGb+pMI2CJAgMBAAECgYBtwpMsOhEpaLOi/FLVY7jreB2M\n" +
            "jTL0iqVDGND82Dtsv/tkWVea+6MlZ/e5tNPSXCDIz5voxpSUmgJzdMyR3XMOiFsz\n" +
            "1iqKHQ3P8oFF1UYeDg0pm+sZXNssLVz21ywMV63CJmEa8FokH30/s306QWB3Gpf+\n" +
            "yzzii5EpYgXPn6ZWUQJBAPvbbfn4o/WcxhN9QybFYKFdcjRG8hkqhqv0jyZsMb+G\n" +
            "+E0IAIbV0wEB8XfkWhyodjJI+lQFg1JUwf02O6qgeLMCQQDy6aFmejl6vncmaazz\n" +
            "vQv7CRyfj26J8Y98UkHnm9X/Gg7YfgHo0UVsgnZPVs0upKIHAjgchIRp9G/DWMMq\n" +
            "oQfTAkBXFuuSkr3vgoSj3J7J21qssel51kaFGUV0VJ7jhGo3dm6KBIhaF7pdENT0\n" +
            "xku6uS4V+VJGl5lvcMubyZMcT10tAkBiKLy6/3sIubzGPf/Qwrz7pXP2mmxNu9qC\n" +
            "n27YW+W+31XOcCTvYtmN/q42Y1oolgiPknsAMyCa/FbJdq3vJQFXAkEAwa6hJcJX\n" +
            "iV7Su/v8Z8wI5uYCSPV3IaIvLWeVM6GIFOz13j1E3i6KNODUEHOn/rpiXGIR/WAX\n" +
            "+aWeC4xeX0SdcQ==";

    private static String encryptRSA(String plainText) {
        RSA rsa = new RSA(EncryptUtil.PRIVATE_KEY_STR, EncryptUtil.KEY);
        byte[] encrypt = rsa.encrypt(StrUtil.bytes(plainText, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
        return new String(encrypt);

    }

    public static String decryptRSADefault(String data) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        RSA rsa = new RSA(EncryptUtil.PRIVATE_KEY_STR, EncryptUtil.KEY);
        byte[] decrypt = rsa.decrypt(data, KeyType.PrivateKey);
        return new String(decrypt);
    }

    public static void main(String[] args) throws Exception{
        RSA rsa = new RSA(EncryptUtil.PRIVATE_KEY_STR, EncryptUtil.KEY);


//        byte[] encrypt = rsa.encrypt(StrUtil.bytes("admin123", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
//        byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
//
//
//        System.out.println(new String(encrypt));
//        System.out.println(new String(decrypt));

        System.out.println(new String(rsa.encrypt("admin123", KeyType.PublicKey)));
        byte[] decrypt1 = rsa.decrypt("VTZaKsf4lNsX1ZtCVfYQtDiwlrXmSA9aOnLyUm1vgE/9Unt954MN6lAKXiXvyySvFS4qk/UBdnQZG7s5/U2O0xwtvFgANPbmUUm6u1eSx3dAS3b+pMHGl5D+4F7jpNeazF2GdmdaecQLM+uAhHWUbCQcvtxyjnoSCROOlfXX5ZE=", KeyType.PrivateKey);
        System.out.println(new String(decrypt1));
    }

}


